package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.ProDao;
import com.jk.dao.ServiceDao;
import com.jk.pojo.ProBean;
import com.jk.pojo.ServiceEntity;
import com.jk.utils.ReturnMsgUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jisiqi
 * @version 1.0
 * @description: TODO
 * @date 2022/10/16 19:34
 */
@Service
public class ProService {
    @Autowired
    private ProDao proDao;

    @Autowired
    private ServiceDao serviceDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    public Map getProList() {
        try {
            QueryWrapper<ProBean> Wrapper = new QueryWrapper<ProBean>();
            List<ProBean> list = proDao.selectList(Wrapper);
            System.out.println("==="+list);
            if (list.size()>0){
                list.forEach(ProBean -> {
                    elasticsearchRestTemplate.save(ProBean);
                });
            }
            return ReturnMsgUtils.returnMsg(200,"查询MySQL服务项目管理,新增es",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public Map queryServiceEsList(ProBean proBean) {
        try {
            List<ProBean> list = new ArrayList<>();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //项目名称模糊查
            if(proBean.getProjectName()!=null && proBean.getProjectName()!=""){
                boolQueryBuilder.must(QueryBuilders.matchQuery("projectName",proBean.getProjectName()));
            }
            //项目状态精确查
            if (proBean.getProjectType()!=null && proBean.getProjectType()!=-1){
                boolQueryBuilder.must(QueryBuilders.termQuery("projectType",proBean.getProjectType()));
            }
            NativeSearchQuery build = new NativeSearchQueryBuilder()
                    .withQuery(boolQueryBuilder)
                    .withHighlightFields(new HighlightBuilder.Field("projectName").preTags("<font color='red'>").postTags("</font>"))
                    .build();
            SearchHits<ProBean> search = elasticsearchRestTemplate.search(build, ProBean.class);
            search.forEach(hit ->{
                ProBean content = hit.getContent();
                Map<String, List<String>> highlightFields = hit.getHighlightFields();
                hit.getContent().setProjectName(highlightFields.get("projectName")==null? hit.getContent().getProjectName():highlightFields.get("projectName").get(0));
                list.add(content);
            });
            return ReturnMsgUtils.returnMsg(200,"es中服务项目管理",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public boolean deleteProId(Integer id) {
        try {
//            proDao.deleteById(id);
            elasticsearchRestTemplate.delete(id.toString(), IndexCoordinates.of("pro"));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @Author: binzi
     * @Description:查询服务包
     * @Date: 2022/10/18 15:08
     * @Return: java.util.List<com.jk.pojo.ServiceEntity>
     **/
    public List<ServiceEntity> findService() {
        QueryWrapper<ServiceEntity> queryWrapper = new QueryWrapper<>();
        return serviceDao.selectList(queryWrapper);
    }
}
